##################################################################
# File : /lua/options/options.lua
# Description : Options definitions
# Author : GPG
##################################################################

--[[
This file contains a table which defines options available to the user in the options dialog

tab data is what populates each tab and defines each option in a tab
Each tab has:
    .title - the text that will appear on the tab
    .key - the key that will group the options (ie in the prefs file gameplay.help_prompts or video.resolution)
    .items - an array of the items for this key (this is an array rather than a genericly keyed table so display order can be imposed)
        .title - the text that will label the option line item
        .tip - the text that will appear in the tooltip for the item
        .key - the prefs key to identify this property
        .default - the default value of the property
        .restart - if true, setting the option will require a restart and the user will be notified
        .verify - if true, prompts the user to veryfiy the change for 15 seconds, otherwise defaults back to prior setting
        .populate - an optional function which when called, will repopulate the options custom data. The value passed in is the current value of the control (function(value))
        .set - an optional function that takes a value parameter and is responsible for determining what happens when the option is applied (function(key, value))
        .ignore - an optional function called when the option is set, checks the value, and won\'t change it from its former setting, and if former setting is invalid, uses return value for new value (function(value))
        .cancel - called when the option is cancelled
        .beginChange - an option function for sliders when user begins modification
        .endChange - an option function for sliders when user ends modification
        .update - a optional function that is called when the control has a new value, also receives the control (function(control, value)), not always used,
                  but if you need additonal control (say of other controls) when this value changes (for example one control may change other controls) you can
                  set up that behavior here
        .type - the type of control used to display this property
            valid types are:
            toggle - multi state toggle button (TODO - add list to replace more than 2 states?)
            button - momentary button (usually open another dialog)
            slider - a value slider
        .custom - a table of data required by the control type, different for each control type.
            slider
                .min - the minimum value for the slider
                .max - the maximum value for the slider
                .inc - the increment between slider detents, if 0 its "analog"
            toggle
                .states - table of states the toggle switch can have
                    .text = text for each state
                    .key = the key or value for each state to be stored in the pref
            button
                .text - the text label of the button

the optionsOrder table is just an array of keys in to the option table, and their order will determine what
order the tabs show in the dialog
--]]

optionsOrder = {
    "GAMEPLAY",
    "VIDEO",
    "SOUND",
}

local savedMasterVol = false
local savedFXVol = false
local savedMusicVol = false
local savedVOVol = false

function PlayTestSound()
    local sound = 'Forge/Interface/UI_Action_MouseDown'
    PlaySound(sound)
end

local voiceHandle = false
function PlayTestVoice()
    if not voiceHandle then
        local sound = 'Forge/COMPUTER_UEF_VO/UEFComputer_MissileLaunch_01358'
        ForkThread(
            function()
                WaitSeconds(0.5)
                voiceHandle = false
            end
        )
        if voiceHandle then
            StopSound(voiceHandle)
        end
        voiceHandle = PlayVoice(sound)
    end
end

options = {
    GAMEPLAY = {
        title = "GAMEPLAY",
        key = 'GAMEPLAY',
        items = {
            {
                title = "<LOC OPTIONS_0001>Zoom Wheel Sensitivity",
                key = 'wheel_sensitivity',
                type = 'slider',
                default = 40,
                set = function(key,value,startup)
                    ConExecute("cam_ZoomAmount " .. tostring(value / 100))
                end,
                custom = {
                    min = 1,
                    max = 100,
                    inc = 0,
                },
            },
            {
                title = "<LOC OPTIONS_0005>Display Tooltips",
                key = 'tooltips',
                type = 'toggle',
                default = true,
                custom = {
                    states = {
                        {text = "<LOC _On>", key = true},
                        {text = "<LOC _Off>", key = false},
                    },
                },
            },
            {
                title = "<LOC OPTIONS_0078>Tooltip Delay",
                key = 'tooltip_delay',
                type = 'slider',
                default = 0,
                set = function(key,value,startup)
                end,
                custom = {
                    min = 0,
                    max = 3,
                    inc = 0,
                },
            },
        },
    },
    VIDEO = {
        title = "VIDEO",
        key = 'VIDEO',
        items = {
            {
                title = "Screen Resolution",
                key = 'primary_adapter',
                type = 'toggle',
                default = '1024,768,60',
                set = function(key,value,startup)
                    if not startup then
                        ConExecute("SC_PrimaryAdapter " .. tostring(value))
                    end
                end,
                verify = true,
                custom = {
                    states = {
                        {text = "1024 x 768", key = 0 },
                        {text = "1280 x 960", key = 1 },
                        {text = "1680 x 1050", key = 2 },
                        {text = "1440 x 900", key = 3 },
                    },
                },
            },
            {
                title = "<LOC OPTIONS_0148>Vertical Sync",
                key = 'vsync',
                type = 'toggle',
                default = 0,
                set = function(key,value,startup)
                    if not startup then
                        ConExecute("SC_VerticalSync " .. tostring(value))
                    end
                end,
                custom = {
                    states = {
                        {text = "<LOC _Off>", key = 0 },
                        {text = "<LOC _On>", key = 1 },
                    },
                },
            },
            {
                title = "<LOC OPTIONS_001>Fidelity Presets",
                key = 'fidelity_presets',
                type = 'toggle',
                default = 4,
                update = function(control,value)
                    logic = import('/lua/options/optionsLogic.lua')
                    if 0 == value then
                        logic.SetValue('fidelity',0,true)
                        logic.SetValue('shadow_quality',1,true)
                        logic.SetValue('texture_level',2,true)
                        logic.SetValue('antialiasing',0,true)
                        logic.SetValue('level_of_detail',0,true)
                    elseif 1 == value then
                        logic.SetValue('fidelity',1,true)
                        logic.SetValue('shadow_quality',2,true)
                        logic.SetValue('texture_level',1,true)
                        logic.SetValue('antialiasing',0,true)
                        logic.SetValue('level_of_detail',1,true)
                    elseif 2 == value then
                        logic.SetValue('fidelity',2,true)
                        logic.SetValue('shadow_quality',3,true)
                        logic.SetValue('texture_level',0,true)
                        logic.SetValue('antialiasing',2,true)
                        logic.SetValue('level_of_detail',2,true)
                    elseif 3 == value then
                        logic.SetValue('fidelity',2,true)
                        logic.SetValue('shadow_quality',3,true)
                        logic.SetValue('texture_level',0,true)
                        logic.SetValue('antialiasing',4,true)
                        logic.SetValue('level_of_detail',2,true)
                    else
                    end
                end,
                set = function(key,value,startup)
                end,
                custom = {
                    states = {
                        { text = "<LOC _Low>", key = 0 },
                        { text = "<LOC _Medium>", key = 1 },
                        { text = "<LOC _High>", key = 2 },
                        { text = "<LOC _Ultra>", key = 3 },
                        { text = "<LOC _Custom>", key = 4 },
                    },
                },
            },
            {
                title = "<LOC OPTIONS_0018>Fidelity",
                key = 'fidelity',
                type = 'toggle',
                default = 2,
                update = function(control,value)
                    import('/lua/options/optionsLogic.lua').SetValue('fidelity_presets',4,true)
                end,
                set = function(key,value,startup)
                    ConExecute("graphics_Fidelity " .. tostring(value))
                end,
                custom = {
                    states = {
                        {text = "<LOC _Low>", key = 0},
                        {text = "<LOC _Medium>", key = 1},
                        {text = "<LOC _High>", key = 2},
                    },
                },
            },
            {
                title = "<LOC OPTIONS_0024>Shadow Fidelity",
                key = 'shadow_quality',
                type = 'toggle',
                default = 3,
                update = function(control,value)
                    import('/lua/options/optionsLogic.lua').SetValue('fidelity_presets',4,true)
                end,
                set = function(key,value,startup)
                    ConExecute("shadow_Fidelity " .. tostring(value))
                end,
                custom = {
                    states = {
                        {text = "<LOC _Off>", key = 0},
                        {text = "<LOC _Low>", key = 1},
                        {text = "<LOC _Medium>", key = 2},
                        {text = "<LOC _High>", key = 3},
                    },
                },
            },
            {
                title = "<LOC OPTIONS_0015>Anti-Aliasing",
                key = 'antialiasing',
                type = 'toggle',
                default = 0,
                update = function(control,value)
                    import('/lua/options/optionsLogic.lua').SetValue('fidelity_presets',4,true)
                end,
                set = function(key,value,startup)
                    if not startup then
                        ConExecute("SC_AntiAliasingSamples " .. tostring(value))
                    end
                end,
                custom = {
                    states = {
                        {text = "<LOC OPTIONS_0029>Off", key = 0},
                        ## the remaining values are populated at runtime from device caps
                        ## what follows is just an example of the data which will be encountered
                        {text =  "2", key =  2},
                        {text =  "4", key =  4},
                        {text =  "8", key =  8},
                        {text = "16", key = 16},
                    },
                },
            },
            {
                title = "<LOC OPTIONS_0019>Texture Detail",
                key = 'texture_level',
                type = 'toggle',
                default = 1,
                update = function(control,value)
                    import('/lua/options/optionsLogic.lua').SetValue('fidelity_presets',4,true)
                end,
                set = function(key,value,startup)
                    ConExecute("ren_MipSkipLevels " .. tostring(value))
                end,
                custom = {
                    states = {
                        { text = "<LOC OPTIONS_0112>Low", key = 2 },
                        { text = "<LOC OPTIONS_0111>Medium", key = 1 },
                        { text = "<LOC OPTIONS_0110>High", key = 0 },
                    },
                },
            },
            {
                title = "<LOC OPTIONS_002>Level Of Detail",
                key = 'level_of_detail',
                type = 'toggle',
                default = 2,
                update = function(control,value)
                    import('/lua/options/optionsLogic.lua').SetValue('fidelity_presets',4,true)
                end,
                set = function(key,value,startup)
                    ConExecute("SC_CameraScaleLOD " .. tostring(value))
                end,
                custom = {
                    states = {
                        { text = "<LOC _Low>", key = 0 },
                        { text = "<LOC _Medium>", key = 1 },
                        { text = "<LOC _High>", key = 2 },
                    },
                },
            },
        },
    },
    SOUND = {
        title = "SOUND",
        items = {
            {
                title = "<LOC OPTIONS_0028>Master Volume",
                key = 'master_volume',
                type = 'slider',
                default = 100,

                init = function()
                    savedMasterVol = GetVolume("Master")
                end,

                cancel = function()
                    SetVolume("Master", savedMasterVol)
                end,

                set = function(key,value,startup)
                    SetVolume("Master", value / 100)
                end,
                update = function(control,value)
                    SetVolume("Master", value / 100)
                end,
                custom = {
                    min = 0,
                    max = 100,
                    inc = 1,
                },
            },
            {
                title = "<LOC OPTIONS_0026>FX Volume",
                key = 'fx_volume',
                type = 'slider',
                default = 100,

                init = function()
                    savedFXVol = GetVolume("SoundFX")
                end,

                cancel = function()
                    SetVolume("SoundFX", savedFXVol)
                end,

                set = function(key,value,startup)
                    SetVolume("SoundFX", value / 100)
                    if not startup then
                        PlayTestSound()
                    end
                end,

                update = function(control,value)
                    SetVolume("SoundFX", value / 100)
                    PlayTestSound()
                end,

                custom = {
                    min = 0,
                    max = 100,
                    inc = 1,
                },
            },
            {
                title = "<LOC OPTIONS_0027>Music Volume",
                key = 'music_volume',
                type = 'slider',
                default = 100,

                init = function()
                    savedMusicVol = GetVolume("Music")
                end,

                cancel = function()
                    SetVolume("Music", savedMusicVol)
                end,

                set = function(key,value,startup)
                    SetVolume("Music", value / 100)
                end,
                update = function(key,value)
                    SetVolume("Music", value / 100)
                end,
                custom = {
                    min = 0,
                    max = 100,
                    inc = 1,
                },
            },
            {
                title = "<LOC OPTIONS_0066>VO Volume",
                key = 'vo_volume',
                type = 'slider',
                default = 100,

                init = function()
                    savedVOVol = GetVolume("VO")
                end,

                cancel = function()
                    SetVolume("VO", savedVOVol)
                end,

                set = function(key,value,startup)
                    SetVolume("VO", value / 100)
                    if not startup then
                        PlayTestVoice()
                    end
                end,
                update = function(key,value)
                    SetVolume("VO", value / 100)
                    PlayTestVoice()
                end,
                custom = {
                    min = 0,
                    max = 100,
                    inc = 1,
                },
            },
        },
    },
}